package com.ruoyi.common.security.feign;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.security.domain.LoginUser;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import io.micrometer.core.instrument.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.time.Instant;
import java.util.*;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.time.Instant;


/**
 * 请求拦截器,拦截请求的参数
 *
 * @author ruoyi
 */
@Component
public class MyTestInterceptor implements RequestInterceptor {
    private static final Logger log = LoggerFactory.getLogger(MyTestInterceptor.class);

//    @Override
//    public void apply(RequestTemplate requestTemplate) {
//        log.info("请求拦截器，目前就知道这个拦截器能获取token，不知道能不能获取传递过来的参数");
//        SecurityContext securityContext = SecurityContextHolder.getContext();
//        log.info("securityContext = {}",securityContext);
//        Authentication authentication = securityContext.getAuthentication();
//        //获取的登录用户这个对象
//        LoginUser lu = (LoginUser)authentication.getPrincipal();
//        //对应的token保存对象
//        Object details = authentication.getDetails();
//        //权限字符集合
//        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
//        authorities.forEach(a->{
//            a.getAuthority();
//        });
//
//        log.info("authentication.getPrincipal() = {}",lu.getUsername());
//        log.info("authentication.getDetails() = {}",authentication.getDetails());
//        log.info("authentication.getAuthorities() = {}",authentication.getAuthorities());
//
//    }
    //只能获取get请求里面的参数和url，对post，delete，put无法拦截（自测，不准）
        @Override
        public void apply(RequestTemplate requestTemplate) {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            Enumeration<String> headerNames = request.getHeaderNames();
            try {
                if (headerNames != null) {
                    while (headerNames.hasMoreElements()) {
                        String name = headerNames.nextElement();
                        //log.info("MyTestInterceptor headerNames(key)："+name);
                        String values = request.getHeader(name);
                        //log.info("MyTestInterceptor headervalues："+values);
                        requestTemplate.header(name, values);
                    }
                }
                log.info("MyTestInterceptor 接口路径："+request.getRequestURL().toString());
                StringBuffer body = new StringBuffer();
                Enumeration<String> bodyNames = request.getParameterNames();
                if (bodyNames != null) {
                    Map map=new HashMap();
                    while (bodyNames.hasMoreElements()) {
                        String name = bodyNames.nextElement();
                        //log.info("MyTestInterceptor bodyNames："+name);

                        String values = request.getParameter(name);
                        //log.info("MyTestInterceptor bodyvalues："+values);
                        requestTemplate.query(name, values);
                        map.put(name,values);
                    }
                    log.info("MyTestInterceptor 传入参数："+map);
                }
                String queryString = request.getQueryString();
                log.info("MyTestInterceptor queryString："+queryString);

                //log.info("MyTestInterceptor request："+request);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
}
